package com.zjh.core.base.basecontroller;

import com.github.pagehelper.Page;
import com.zjh.common.dtgrid.model.Pager;
import com.zjh.common.util.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lk
 * @Description: controller基类
 * @date 2016/3/2 03:22
 */
public abstract class BaseController {

    public Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 设置分页模型基本信息
     *
     * @param request
     * @param page
     */
    protected void setPage(HttpServletRequest request, PageUtil page) {
        page.setPageNum(Integer.valueOf(request.getParameter("page")));
        page.setPageSize(Integer.valueOf(request.getParameter("rows")));
        page.setOrderByColumn(request.getParameter("sidx"));
        page.setOrderByType(request.getParameter("sord"));
    }


    /**
     * 设置分页信息以及分页数据list
     *
     * @param parameters
     * @param pager
     * @param page
     * @param list
     * @param <T>
     */
    protected <T> void setPageList(Map<String, Object> parameters, Pager pager,
                                   Page<Object> page, List<T> list) {
        parameters.put("isSuccess", Boolean.TRUE);
        parameters.put("nowPage", pager.getNowPage());
        parameters.put("pageSize", pager.getPageSize());
        parameters.put("pageCount", page.getPages());
        parameters.put("recordCount", page.getTotal());
        parameters.put("startRecord", page.getStartRow());
        //列表展示数据
        parameters.put("exhibitDatas", list);
    }


    /**
     * 设置前端分页信息以及分页数据list
     *
     * @param parameters
     * @param pager
     * @param page
     * @param list
     * @param <T>
     */
    /*protected <T> void setFrontPageList(Map<String, Object> parameters, FrontPager pager,
                                        Page<Object> page, List<T> list) {
        parameters.put("nowPage", pager.getNowPage());
        parameters.put("pageSize", pager.getPageSize());
        parameters.put("pageCount", page.getPages());
        parameters.put("recordCount", page.getTotal());
        parameters.put("startRecord", page.getStartRow());
        //展示数据
        parameters.put("exhibitDatas", list);
    }*/



    /**
     * 获取快速查询传递的参数
     *
     * @param fastParameters
     * @return
     */
    protected Map<String, Object> getFastqueryParameters(Map<String, Object> fastParameters) {
        //使用新的map来存放查询条件
        Map<String, Object> tempMap = new HashMap<>(16);
        //遍历快速查询中的key,并且用去除lk_后的key替换之
        for (String key : fastParameters.keySet()) {
            //新的key不包含lk_前缀
            String newKey = key.replace("lk_", "");
            tempMap.put(newKey, fastParameters.get(key));
        }
        return tempMap;
    }


}